package com.fsnmt.maizhuanbao.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/** 截图工具类
 *  android.Manifest.permission.WRITE_EXTERNAL_STORAGE permission. 
    android.Manifest.permission.READ_EXTERNAL_STORAGE permission.
 */
public class PictureTool {
	private final static String ALBUM_PATH = Environment.getExternalStorageDirectory().toString();
	
	
	/**
	 * 保存图片文件到手机中
	 * @param bm
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public  static Boolean saveFile(Bitmap bm, String fileName,String dirName) throws IOException {
		File dirFile = new File(ALBUM_PATH+"/"+dirName+"/");
		if (!dirFile.exists()) {
			dirFile.mkdir();
		}
		boolean fileIsExist = false;
		File picFile = new File(ALBUM_PATH +"/"+dirName+"/"+ fileName);
		if (!picFile.exists()) {
			fileIsExist = false;
			BufferedOutputStream stream = new BufferedOutputStream(
					new FileOutputStream(picFile));
			bm.compress(Bitmap.CompressFormat.JPEG, 80, stream);
			stream.flush();
			stream.close();
			fileIsExist = true;
		} else {
			fileIsExist = true;
		}
		return fileIsExist;
	}
	
	
	
	/**
	 * 图片插入相册
	 * 
	 * @param fielName
	 *            图片名称
	 */
	public static void insertImage(Context context ,String fielName,String dirName) {
		String mBitmap = ALBUM_PATH +"/"+dirName+"/"+ fielName;
//		String url = null;
		try {
			MediaStore.Images.Media.insertImage(context.getContentResolver(),
					mBitmap, "", "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		ContentValues localContentValues = new ContentValues();
		localContentValues.put("_data",mBitmap);
		localContentValues.put("description", "save image ---");
		localContentValues.put("mime_type", "image/png");
		ContentResolver localContentResolver = context.getContentResolver();
		Uri localUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		localContentResolver.insert(localUri, localContentValues);
	}
}
